// pages/particulars/particulars.js
import classify from "../../utils/classify"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        item: {},
        arrlength: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options, params) {
        this.setitem(options.cid, params)
        this.getcarlength()
    },
    async setitem(cid, params) {
        let res = await classify('/answer/curriculum', params, "", 'GET')
        res.forEach(item => {
            JSON.parse(item.teacherClassList).forEach(it => {
                if (it.cid === cid) {
                    it.teacher = item.teacher
                    it.teacherImg = item.teacherImg
                    it.id = item.id
                    this.setData({
                        item: it
                    })
                }
            })
        })
        this.getcarlength()
    },
    // 修改收藏状态
         async setcollect(){
                let cid = this.data.item.cid
                let id = this.data.item.id
                let res = await classify('/answer/setcollect',{id,cid},"",'GET')
                this.setitem(cid)
            },
    // 跳转购物车
    tocar() {
        wx.reLaunch({
            url: '/pages/car/car',
        })
    },
    //添加购物车
    addcar() {
        this.data.item.num += 1
        let item = this.data.item
        classify('/answer/addcar', {
            item
        }, "", 'POST')
        // this.getcarlength()
    },
    // 获取购物车数量
    async getcarlength(params) {
        let res = await classify('/answer/getcar', params, "", 'GET')
        this.setData({
            arrlength: res.length
        })
    },

    // 立即购买
    mai() {
        let {cid} = this.data.item
        console.log("购买");
        wx.navigateTo({
            url:`/pages/confirmOrder/confirmOrder?id=${cid}`,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})